package com.hwd.gmall.seckill.config;

import com.hwd.gmall.common.constant.SeckillConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 秒杀下单的的rabbit配置类
 *
 * @author 黄伟东/Victor
 * @date 2022/5/10 22:15
 */
@Configuration
public class SeckillAddOrderRabbitConfig {

    /**
     * 创建秒杀下单交换机
     *
     * @return org.springframework.amqp.core.Exchange
     */
    @Bean("seckillOrderExchange")
    public Exchange seckillOrderExchange() {
        return ExchangeBuilder.directExchange(SeckillConst.SECKILL_ORDER_EXCHANGE).build();
    }

    /**
     * 创建秒杀下单队列
     *
     * @return org.springframework.amqp.core.Queue
     */
    @Bean("seckillOrderQueue")
    public Queue seckillOrderQueue() {
        return QueueBuilder.durable(SeckillConst.SECKILL_ORDER_QUEUE).build();
    }

    /**
     * 秒杀下单交换机和队列进行绑定
     *
     * @param orderPayExchange 订单支付交换机
     * @param orderPayQueue    订单支付队列
     * @return org.springframework.amqp.core.Binding
     */
    @Bean
    public Binding seckillOrderBinding(@Qualifier("seckillOrderExchange") Exchange seckillOrderExchange,
                                   @Qualifier("seckillOrderQueue") Queue seckillOrderQueue) {
        return BindingBuilder.bind(seckillOrderQueue).to(seckillOrderExchange).with(SeckillConst.SECKILL_ORDER_ROUTING_KEY).noargs();
    }
}
